

//
//  CPService.swift
//  ComponentNetwork
//
//  Created by wrs on 2018/7/18.
//  Copyright © 2018年 wrs. All rights reserved.
//

import UIKit
import Alamofire

public typealias CPMethod = Alamofire.HTTPMethod

public typealias CPRespHandle = (_ suc: Bool, _ data: String?, _ error: Error?, _ responseHeader: [AnyHashable : Any]?) -> Void
public typealias CPProgressHandle = (_ progress: Progress) -> Void

public typealias CPDownloadRespHandle = (_ suc: Bool, _ resp: Data?, _ destinationURL: URL?, _ error: Error?, _ responseHeader: [AnyHashable : Any]?) -> Void


/// 网络接口请求
/// - Parameters:
///   - params: 参数
///   - respHandle: 回调
public func CPRequest(params: CPRequestParams, respHandle: CPRespHandle? = nil) {
    AFRequest(url: params.url, requestMethod: params.method, params: params.params, header: params.headers, respHandle: respHandle, cer: params.cer)
}

public func CPUploadFile(params: CPRequestParams, file: AnyObject?, fileName: String, key: String, mimeType: String, uploadingHandle:@escaping CPProgressHandle, respHandle: CPRespHandle? = nil) -> Void {
    AFUploadFile(url: params.url, header: params.headers, params: params.params, file: file, fileName: fileName, key: key, mimeType: mimeType, uploadingHandle: uploadingHandle, respHandle: respHandle)
}

// MARK: -

public func CPRequest(url: String, requestMethod: CPMethod, params: [String: Any]? = nil, header: [String: String]? = nil, respHandle: CPRespHandle? = nil, cer: Data? = nil) {
    AFRequest(url: url, requestMethod: requestMethod, params: params, header: header, respHandle: respHandle, cer: cer)
}


public func CPUploadFile(url: String, header: [String: String]? = nil, params: [String: Any]? = nil, file: AnyObject, fileName: String, key: String, mimeType: String, uploadingHandle:@escaping CPProgressHandle, respHandle: CPRespHandle? = nil) -> Void {
    AFUploadFile(url: url, header: header, params: params, file: file, fileName:fileName, key:key, mimeType:mimeType, uploadingHandle:uploadingHandle, respHandle:respHandle)
}

public func CPDownloadFile(url: String, header: [String: String]? = nil, filePath:URL, downloaDingHandle: CPProgressHandle? = nil, respHandle: CPDownloadRespHandle? = nil) -> Void {
    AFDownloadFile(url: url, header: header, filePath: filePath, downloaDingHandle: downloaDingHandle, respHandle: respHandle)
}
